package game.entity.ground;

import edu.monash.fit2099.engine.Actions;
import edu.monash.fit2099.engine.Actor;
import edu.monash.fit2099.engine.Ground;
import edu.monash.fit2099.engine.Location;
import game.CoinPoint;
import game.entity.action.EatAction;
import game.entity.action.SearchAction;
import game.entity.actor.Brachiosaur;
import game.entity.actor.Player;
import game.entity.item.Fruit;
import game.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;

public class Tree extends Ground {
    private int age = 0;
    private List<Fruit> fruits = new ArrayList<>();
    private int dropRate;
    //找到水果的概率
    private final int FIND_FRUIT_RATE = 60;

    public Tree() {
        super('+');
        dropRate = 5;
    }

    public Tree(char displayChar, int dropRate) {
        super(displayChar);
        this.dropRate = dropRate;
    }

    @Override
    public Actions allowableActions(Actor actor, Location location, String direction) {
        Actions actions = new Actions();
        if(actor instanceof Brachiosaur && hasFruits()){
            actions.add(new EatAction(fruits.get(0)));
        }
        if(actor instanceof Player){
            actions.add(new SearchAction(FIND_FRUIT_RATE, this));
        }
        return actions;
    }

    @Override
    public void tick(Location location) {
        age++;
        if (age == 10)
            displayChar = 't';
        if (age == 20)
            displayChar = 'T';
//任何树木有50%的机会产生一个成熟的水果
        if (RandomUtil.shotOn(50)) {
            fruits.add(new Fruit("fruit" + RandomUtil.randomInt()));
            CoinPoint.addPoints(1,"Tree grow fruits");
        }

        for (Fruit fruit : fruits) {
            if (RandomUtil.shotOn(dropRate)) {
//        树上任何成熟的水果都有几率掉落（比如5%）
                location.addItem(fruit);
            }
        }

    }

    //树上是否有水果
    public boolean hasFruits() {
        return fruits.size() > 0 ? true : false;
    }
}
